package com.usian.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.common.exception.CatchCustomException;
import com.usian.common.exception.CustomException;
import com.usian.common.exception.code.AdminStatusCode;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.utils.threadlocal.AdminThreadLocalUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper,AdChannel> implements AdChannelService {

    /**
     * 根据条件分页查询
     */
    @Override
    public PageResponseResult findBySearch(ChannelDto channelDto) {
        // 只用是admin用户才能使用这个方法（只要登录用户才能访问）
        // 1. 解析token  2. 获得用户Id  根据用户Id 查询用户信息
        // 过滤器
        AdUser adUser =  AdminThreadLocalUtils.getUser();
        System.out.println(adUser.getId());

        // 1. 判断dto对象 为空
        if(channelDto==null){
            return (PageResponseResult)ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 1.1 初始化 分页参数
        channelDto.checkParam();
        // 2. 封装查询条件
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(channelDto.getName()!=null&&!"".equals(channelDto.getName()),
                AdChannel::getName,channelDto.getName());
        // 3. 执行分页操作
        Page<AdChannel> page = new Page<>(channelDto.getPage(),channelDto.getSize());
        IPage<AdChannel> iPage = this.page(page,lambdaQueryWrapper);
        // 4. 封装分页结果
        PageResponseResult pageResponseResult = new PageResponseResult(iPage.getCurrent(),iPage.getSize(),iPage.getTotal());
        pageResponseResult.setData(iPage.getRecords());
        // 5. 返回
        return pageResponseResult;
    }

    /**
     * 添加频道信息
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult saveAdchannel(AdChannel adChannel) {
        //为了代码健壮性 需要判断
        if(adChannel == null){
            CatchCustomException.catchs(AdminStatusCode.NAMEFALL,"参数不能为空");
        }
        if(adChannel.getName() == null || "".equals(adChannel.getName())){
            throw new CustomException(AdminStatusCode.NAMEFALL,"用户名不能为空！");
        }
        // 添加
        // 维护初始化字段
        adChannel.setCreatedTime(new Date());
//        adChannel.setIsDefault(adChannel.getIsDefault()?true:false);
//        adChannel.setOrd(adChannel.getOrd()==null?0:adChannel.getOrd());
        this.save(adChannel);
        int i = 9/0;
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道信息
     * @param id
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(Integer id, AdChannel adChannel) {
        // 1. 检查参数
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 2. 查询是否有修改对象
        AdChannel adChannel1 =  this.getById(id);
        // 4. 没有 --》 失败
        if(adChannel1 ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 3. 有  -》修改
        // 设置Id
        adChannel.setId(id);
        this.saveOrUpdate(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据ID 删除频道信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        // 1. 检查参数
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 直接删除
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findAll() {
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdChannel::getStatus,1);
        lambdaQueryWrapper.orderByDesc(AdChannel::getOrd);
        List<AdChannel> list =  this.list(lambdaQueryWrapper);
        return ResponseResult.okResult(list);
    }
}
